package com.jingdianjichi.club.oss.util;

import com.jingdianjichi.club.oss.enty.FileInfo;
import io.minio.*;
import io.minio.errors.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * minio连接工具类
 * @author 86156
 * @version 1.0
 * @date 2024-10-25 9:33
 */
@Component
public class MinioUtil {

    @Resource
    private MinioClient minioClient;

    /**
     * minio连接的Url
     */
    @Value("${minio.url}")
    private String url;


    /**
     * 创建bucket
     */
    public void createBucket(String bucketName) throws Exception {
        boolean bucketExists = minioClient.
                bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if(!bucketExists){
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }


    /**
     * 上传文件
     */
    public void uploadFile(InputStream inputStream,String bucketName,String objectName) throws Exception{
        minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).
                stream(inputStream,-1,Integer.MAX_VALUE).build());
    }

    /**
     * 列出所有的 bucket
     */
    public List<String> listBucket() throws Exception {
        List<Bucket> buckets = minioClient.listBuckets();
        return buckets.stream().map(Bucket::name).collect(Collectors.toList());
    }

    /**
     * 获取当前bucket下的所有文件
     */
    public List<FileInfo> getAllFile(String bucketName) throws Exception{
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
        List<FileInfo> fileInfoList = new LinkedList<>();
        for (Result<Item> result : results) {
            Item item = result.get();
            FileInfo fileInfo = new FileInfo();
            fileInfo.setFileName(item.objectName());
            fileInfo.setDirectoryFlag(item.isDir());
            fileInfo.setEtag(item.etag());
            fileInfoList.add(fileInfo);
        }
        return fileInfoList;
    }

    /**
     * 下载文件
     */
    public InputStream downLoadFile(String bucketName,String objectName) throws Exception{
        return  minioClient.getObject(GetObjectArgs.builder()
                .bucket(bucketName).object(objectName).build());
    }

    /**
     * 删除桶
     */
    public void deleteBucket(String bucketName) throws Exception{
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }

    /**
     * 删除文件
     */
    public void deleteFile(String bucketName,String objectName) throws Exception{
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
    }

    /**
     * 获取文件url地址
     */
    public String getObjectUrl(String bucketName, String objectName) throws Exception{
        return url+"/"+bucketName+"/"+objectName;
    }
}
